﻿<%@ WebHandler Language="C#" Class="user" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Linq;

using Newtonsoft.Json;
using VISS.Web.Frontend;
using NCT.Framework;
using VISS.API.Users;
using VISS.MailImpl;
using VISS.API.Commons;

public class user : IHttpHandler, IRequiresSessionState {

    IProperty _iProperty = StructureMapFactory.GetInstance<IProperty>();
    IUserProperty _iUserProperty = StructureMapFactory.GetInstance<IUserProperty>();
    protected UserContext userContext;
    public void ProcessRequest(HttpContext context) {
        string action = context.Request.Form["action"];
        switch (action) {
            case "CheckSendGif":
                CheckSendGif(context);
                break;
            case "SendGif":
                SendGif(context);
                break;   
            default:                
                break;         
        }
    }
    
    private void CheckSendGif(HttpContext context)
    {
        string result = "{{\"result\":\"{0}\",\"items\":{1}}}";
        int proID = ConvertUtils.ToInt(context.Request.Form["proID"]);
        PropertyEnt item = _iProperty.GetByID(proID);
        if (item != null)
            FormValidateUtils.Return(string.Format(result, "true", SerializerUtils.JsonSerialize(item)));
        else
            FormValidateUtils.Return(string.Format(result, "false"));
    }

    private void SendGif(HttpContext context)
    {
        string result = "{{\"result\":\"{0}\",\"items\":\"{1}\"}}";
         int proID = ConvertUtils.ToInt(context.Request.Form["proID"]);
         string userKey = ConvertUtils.ToString(context.Request.Form["reID"]);
        PropertyEnt item = _iProperty.GetByID(proID);
        userContext = LoginFactory.GetCurrentUser();
        if (userContext != null && (userContext.UserKey != userKey))
        {
            EUserPropertySendPropertyResult results = _iUserProperty.SendProperty(userContext.UserKey, userKey, item, "");
            if(results==EUserPropertySendPropertyResult.Success)
                FormValidateUtils.Return(string.Format(result, "true","true"));
            if (results == EUserPropertySendPropertyResult.NotEnoughPoint)
                FormValidateUtils.Return(string.Format(result, "false", "NotEnoughPoint"));
            if (results == EUserPropertySendPropertyResult.NotEnoughCoin)
                FormValidateUtils.Return(string.Format(result, "false", "NotEnoughCoin"));
        }
        else
            FormValidateUtils.Return(string.Format(result, "false","false")); 
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }
}
